package restPROC;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;




import javax.ws.rs.core.Context;

import dao.daoKonekcija;
import dao.daoPermisije;
import dao.daoRole;
import utilClasses.ListaRole;

@Path("/roles") 
public class Roles {
	@GET
	@Path("/user")
	@Produces({"application/xml"})
	public ListaRole getUsers(@QueryParam("korisnik") String korisnik,@Context HttpServletRequest req)
	{
		ListaRole rez = new ListaRole();
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "getRoles");
			System.out.println(jelima);
			if(!jelima) return rez;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			rez=daoRole.dajRole(korisnik);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}
	@GET
	@Path("/kreiraj")
	@Produces("text/plain")
	public String kreiraj(@QueryParam("rola") String naziv,@QueryParam("korisnik") String korisnik,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(korisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "insertRoles");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String rez = null;
		try {
			
			rez=daoRole.insertRola(naziv,korisnik);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}
	@GET
	@Path("/moje")
	@Produces({"application/xml"})
	public ListaRole getMojeRole(@QueryParam("korisnik") String korisnik)
	{

		ListaRole lr=null;
		try {
			
			lr=daoRole.dajSamoRoleKojeImam(korisnik);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lr;
	}
	@GET
	@Path("/broj")
	@Produces("text/plain")
	public String getBrojRola(@QueryParam("korisnik") String korisnik){
		String rez=new String(Integer.toString(0));
		try {
			
			rez=Integer.toString(daoRole.brojRola(korisnik));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
	}
	@GET
	@Path("/povezi")
	@Produces("text/plain")
	public String kreiraj(@QueryParam("rola") String naziv,@QueryParam("korisnik") String korisnik,@QueryParam("brisem") String brisem,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(korisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "insertRoles");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String rez = null;
		try {
			
			rez=daoRole.updateUserRola(naziv,korisnik,brisem);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}

}
